import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_widgets/resources/kq_theme_colors.dart';
import 'package:kq_flutter_widgets/widgets/button/kq_outline_button.dart';
import 'package:kq_flutter_widgets/widgets/chart/base/kq_chart_model.dart';
import 'package:kq_flutter_widgets/widgets/chart/kq_chart_line.dart';
import 'package:kq_flutter_widgets/widgets/chart/kq_chart_line_model.dart';
import 'package:kq_flutter_widgets/widgets/titleBar/kq_title_bar.dart';

class KqChartLineDemo extends StatefulWidget {
  const KqChartLineDemo({Key? key}) : super(key: key);
  @override
  State<KqChartLineDemo> createState() => _KqChartLineDemoState();
}

class _KqChartLineDemoState extends State<KqChartLineDemo> {
  List<ChartLineValue> chartList = [
    ChartLineValue(xValue: '周一', yValue: '20'),
    ChartLineValue(xValue: '周二', yValue: '40'),
    ChartLineValue(xValue: '周三', yValue: '30'),
    ChartLineValue(xValue: '周四', yValue: '60'),
    ChartLineValue(xValue: '周五', yValue: '50'),
    ChartLineValue(xValue: '周六', yValue: '90'),
    ChartLineValue(xValue: '周末', yValue: '70')
  ];

  List<ChartLineValue> chartList1 = [
    ChartLineValue(xValue: '周一', yValue: '30'),
    ChartLineValue(xValue: '周二', yValue: '30'),
    ChartLineValue(xValue: '周三', yValue: '20'),
    ChartLineValue(xValue: '周四', yValue: '40'),
    ChartLineValue(xValue: '周五', yValue: '70'),
    // ChartLineValue(xValue: '周六', yValue: '60'),
  ];
  List<ChartLineValue> chartList2 = [
    ChartLineValue(
      xValue: '一月',
      yValue: '30',
      lineWidth: 2,
    ),
  ];

  ChartLineModel model = ChartLineModel(
    chartModel: KqChartModel(
        sizeHeight: 300.r, sizeWidth: 400.r, xRight: 30.r, isFixMaxY: true),
  );

  ChartLineModel model1 = ChartLineModel(
    chartModel: KqChartModel(
        sizeHeight: 300.r,
        sizeWidth: 410.r,
        xRight: 20.r,
        isFixMaxY: false,
        xFont: 11.sp),
  );

  List<List<ChartLineValue>> chartArr = [];
  bool isRefresh = false;

  @override
  void initState() {
    super.initState();
    model.chartModel.isCurve = true;
    model.chartModel.isShowPoint = false;
    model.chartModel.xLeft = 50.r;
    model.chartModel.maxY = 100;
    model.chartModel.isAvgValue = true;
    model.chartModel.isAvg = true;
    model.chartModel.unit = '单位:%';
    model.lineColors = [KqThemeColors.textYellow, KqThemeColors.textRed];
    model.shaderColors = [
      [KqThemeColors.textYellow, KqThemeColors.bgWhite],
      [KqThemeColors.textRed, KqThemeColors.bgWhite]
    ];
    model.chartModel.initialPointX = 30.r;
    model.chartModel.avgColors = [
      KqThemeColors.textBlue,
      KqThemeColors.textGreen
    ];
    // model.shaderColors = [
    //   [
    //     KqThemeColors.textYellow,
    //     KqThemeColors.bgWhite,
    //   ]
    // ];

    // chartArr = [chartList1, chartList];

    model1.chartModel.maxY = 100;
    model1.chartModel.isCurve = false;
    model1.chartModel.xLeft = 40.r;
    model1.chartModel.yBottom = 50.r;
    model1.chartModel.isAddMask = true;
  }

  @override
  Widget build(BuildContext context) {
    // print('----------------1');
    return Scaffold(
        appBar: KqHeadBar(
          headTitle: '图表',
          back: () {
            Get.back();
          },
        ),
        body: ListView(
          children: [
            KqChartLine(
              chartModel: model,
              height: 300.r,
              backGroundColor: KqThemeColors.bgWhite,
              keyTag: "2",
              valueList: [chartList, chartList1],
            ),
            // SizedBox(height: 10.r),
            // KqChartLine(
            //   chartModel: model1,
            //   valueList: [chartList2],
            //   keyTag: "1",
            //   backGroundColor: KqThemeColors.bgWhite,
            // ),
            KqOutlineButton(
                title: '刷新',
                onTap: () {
                  model.chartModel.isCurve = !model.chartModel.isCurve;
                  if (chartArr.isEmpty) {
                    chartArr.add(chartList);
                  } else {
                    chartArr = [];
                  }
                  setState(() {});
                })
          ],
        ));
  }
}
